//
//  SEGBillSelectItemViewController.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2019/3/5.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGBillSelectItemViewController.h"
#import "SEGBillSelectItemTableViewCell.h"

@interface SEGBillSelectItemViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) BOOL isTapConfirmButton; ///< 是否点击了确定按钮
@end

@implementation SEGBillSelectItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self handleData];
    
    if (self.feeItemArray.count == 0) {
        [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.view, @"text":kNoDataDefaultMessage}];
    }
}

- (void)setupUI {
    self.title = @"缴费科目";
    
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = kColor_bg;
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.rowHeight = 44;
        [tableView registerClass:[SEGBillSelectItemTableViewCell class] forCellReuseIdentifier:@"SEGBillSelectItemTableViewCell"];
        tableView.separatorColor = kColor_L3;
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, -15);
        [self.view addSubview:tableView];
        tableView;
    });
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    UIButton *button = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:kColor_1 forState:UIControlStateNormal];
        button.titleLabel.font = kFont_13;
        [button addTarget:self action:@selector(confirmButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
}

// 处理数据
- (void)handleData {
    // 解决第一次进来选择了项目但是没点击确定，第二次进来选择状态没清除的问题
    if (self.selectFeeItemArray.count == 0) {
        for (SEGBillFeeItemModel *itemModel in self.feeItemArray) {
            itemModel.selected = NO;
        }
        [self.tableView reloadData];
    }
}

#pragma Action
- (void)confirmButtonTap:(UIButton *)sender {
    self.isTapConfirmButton = YES;
    if (self.confirmBlock) {
        NSMutableArray<SEGBillFeeItemModel *> *temp = [NSMutableArray array];
        for (SEGBillFeeItemModel *itemModel in self.feeItemArray) {
            if (itemModel.selected) {
                [temp addObject:itemModel];
            }
        }
        self.confirmBlock([temp copy]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.feeItemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGBillSelectItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SEGBillSelectItemTableViewCell" forIndexPath:indexPath];
    SEGBillFeeItemModel *model = self.feeItemArray[indexPath.row];
    cell.radio.selected = model.selected;
    cell.titleLabel.text = model.feeItemTypeName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGBillSelectItemTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SEGBillFeeItemModel *model = self.feeItemArray[indexPath.row];
    model.selected = !model.selected;
    NSMutableArray *temp = [NSMutableArray arrayWithArray:self.feeItemArray];
    temp[indexPath.row] = model;
    cell.radio.selected = model.selected;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
